﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Cashier_Service;
using Cashier_Interface;
using Cashier.utils;
using Cashier_Utils;
using Cashier.shipment;

namespace Cashier.finance
{
    public partial class DebtBillShipmentControl : UserControl, IDataGridViewData, IReloadData, IPrinter
    {
        private MainForm mainform;
        private static DebtBillShipmentControl debtBillShipmentControl;
        private BillShipmentService shipmentService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mainform"></param>
        private DebtBillShipmentControl(MainForm mainform)
        {
            InitializeComponent();
            this.mainform = mainform;
            this.dataGridView1.ImplementsInstance = this;
            this.shipmentService = new BillShipmentService();

            this.ControlsSetRights();
            this.Dock = DockStyle.Fill;
        }

        /// <summary>
        /// 获取ShipmentListControl实例
        /// </summary>
        /// <param name="mainform"></param>
        /// <returns></returns>
        public static DebtBillShipmentControl GetDebtBillShipmentListControl(MainForm mainform)
        {
            if (debtBillShipmentControl == null)
                debtBillShipmentControl = new DebtBillShipmentControl(mainform);
            debtBillShipmentControl.RefreshDataGridViewData();
            return debtBillShipmentControl;
        }

        #region IDataGridViewData 成员

        void IDataGridViewData.EnterkeyEventHandler()
        {
            this.ShowRepaymentForm();
        }

        void IDataGridViewData.GenerateGridViewColumns(DataGridView dgv)
        {
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.Columns.Clear();
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("id", null, true, 0, null, null, false, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("shipment_no", "出货单号", true, 12, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("numbers", "商品数量", true, 6, ActKey.RIGHT, ActKey.RIGHT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("goods_total", "商品总金额", true, 10, ActKey.RIGHT, ActKey.RIGHT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("shipment_total", "货单总金额", true, 8, ActKey.RIGHT, ActKey.RIGHT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("discount_total", "总优惠金额", true, 8, ActKey.RIGHT, ActKey.RIGHT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("payway", "支付方式", true, 8, ActKey.CENTER, ActKey.CENTER, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("pay_amount", "付款金额", true, 8, ActKey.RIGHT, ActKey.RIGHT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("give_amount", "找零金额", true, 8, ActKey.RIGHT, ActKey.RIGHT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("debt_amount", "欠款金额", true, 8, ActKey.RIGHT, ActKey.RIGHT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("member_no", "出货客户", true, 8, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("admin_name", "出货人", true, 8, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("shipment_date", "出货日期", true, 8, ActKey.CENTER, ActKey.CENTER, true, false));
        }

        void IDataGridViewData.GetBindingData(int rowindex, int pageindex, int pagesize)
        {
            try
            {
                String enterpriseNo = mainform.Admin.EnterpriseNo;
                String adminUsername = mainform.UsernameForQuery;
                String shipmentno = textbox_shipmentno.Text.Trim();
                String memberno = textbox_memberno.Text.Trim();

                DataTable dt = shipmentService.GetBillShipmentTable(pageindex, pagesize, enterpriseNo, null, shipmentno, memberno,
                    adminUsername, null, null, 0.01M, null, "id");
                this.dataGridView1.DataSource = dt;
                if (dt != null && dt.Rows.Count > 0)
                {
                    if (rowindex > 0)
                        dataGridView1.Rows[0].Selected = false;
                    dataGridView1.Rows[rowindex].Selected = true;
                }
                dataGridView1.Columns[0].Visible = false;

                mainform.SetPageInfo(dataGridView1.Recordcount, pageindex, pagesize);
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        long IDataGridViewData.GetBindingDataRecord()
        {
            long record = 0;
            try
            {
                String enterpriseNo = mainform.Admin.EnterpriseNo;
                String adminUsername = mainform.UsernameForQuery;
                String shipmentno = textbox_shipmentno.Text.Trim();
                String memberno = textbox_memberno.Text.Trim();
                
                record = shipmentService.GetBillShipmentRecord(enterpriseNo, null, shipmentno, memberno, adminUsername, null, null, 0.01M);
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
            return record;
        }

        #endregion

        #region 刷新数据

        /// <summary>
        /// 刷新数据
        /// </summary>
        public void RefreshDataGridViewData()
        {
            this.dataGridView1.LoadingData();
        }

        #endregion

        #region 控件事件

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_query_Click(object sender, EventArgs e)
        {
            this.RefreshDataGridViewData();
        }

        /// <summary>
        /// 客户还款
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_rePayment_Click(object sender, EventArgs e)
        {
            this.ShowRepaymentForm();
        }

        /// <summary>
        /// 查看商品
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem_view_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count > 0)
            {
                String shipmentno = this.dataGridView1.SelectedRows[0].Cells["shipment_no"].Value.ToString();
                this.ViewBillShipmentGoods(shipmentno);
            }
        }

        /// <summary>
        /// 查看还款记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem_repayment_query_Click(object sender, EventArgs e)
        {
            this.RepaymentsQuery();
        }

        /// <summary>
        /// 还款
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem_rePayment_Click(object sender, EventArgs e)
        {
            this.ShowRepaymentForm();
        }

        /// <summary>
        /// DataGridView 单元格双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            this.ShowRepaymentForm();
        }

        #endregion

        #region 功能方法实现

        /// <summary>
        /// 控件设置权限
        /// </summary>
        private void ControlsSetRights()
        {
            Button[] buttons = { button_query, button_rePayment };
            RightsforControls.RightsforButton(mainform.Admin.RolePurviewtable, buttons);
            ContextMenuStrip[] menuStrips = { contextMenuStrip_dgv };
            RightsforControls.RightsforContextMenuStrip(mainform.Admin.RolePurviewtable, menuStrips);
        }

        /// <summary>
        /// 打开还款窗口
        /// </summary>
        private void ShowRepaymentForm()
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                DataGridViewRow dgvRow = dataGridView1.SelectedRows[0];
                decimal debtAmount = (decimal)dgvRow.Cells["debt_amount"].Value;
                DebtBillShipmentAddForm shipmentAddForm = new DebtBillShipmentAddForm(mainform, dgvRow, debtAmount, this);
                shipmentAddForm.ShowDialog();
            }
        }

        /// <summary>
        /// 查询还款记录
        /// </summary>
        private void RepaymentsQuery()
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                String shipmentno = dataGridView1.SelectedRows[0].Cells["shipment_no"].Value.ToString();
                BillShipmentRepaymentControl repaymentControl = BillShipmentRepaymentControl.GetBillShipmentRepaymentControl(mainform, shipmentno);
                mainform.AddDataGridViewPanelControl(repaymentControl);
            }
        }

        /// <summary>
        /// 查看出货单商品
        /// </summary>
        /// <param name="shipmentno"></param>
        private void ViewBillShipmentGoods(String shipmentno)
        {
            ShipmentGoodsListControl shipmentGoodsListControl = ShipmentGoodsListControl.GetShipmentGoodsListControl(mainform, shipmentno);
            this.mainform.AddDataGridViewPanelControl(shipmentGoodsListControl);
        }

        #endregion

        #region 重写方法

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool b = base.ProcessCmdKey(ref msg, keyData);
            if (keyData == Keys.Up || keyData == Keys.Down)
            {
                this.dataGridView1.Focus();
            }
            else if (keyData == Keys.F8)
            {
                this.RefreshDataGridViewData();
            }
            return b;
        }

        #endregion


        #region IReloadData 成员

        void IReloadData.ReloadDataGridViewData(object param)
        {
            this.RefreshDataGridViewData();
        }

        #endregion


        #region IPrinter 成员

        DataGridView IPrinter.GetAllRows()
        {
            try
            {
                ((IDataGridViewData)this).GetBindingData(0, 0, 0);
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
            return this.dataGridView1;
        }

        DataGridView IPrinter.GetSelectedRows()
        {
            return this.dataGridView1;
        }

        void IPrinter.VisibleColumn(string columnname)
        {
            try
            {
                if (this.dataGridView1.Columns[columnname].Visible)
                    this.dataGridView1.Columns[columnname].Visible = false;
                else
                    this.dataGridView1.Columns[columnname].Visible = true;
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        #endregion
    }
}
